import { ref } from 'vue'
import { defineStore } from 'pinia'
import { useRouter } from 'vue-router'

export const useCounterStore = defineStore('counter', () => {
  var $router = useRouter() ;
    var tasklist = ref([]);
    var addTask = (payload)=>{
      tasklist.value.push( { id: tasklist.value.length+1, name:payload, status:false } );
      $router.go(-1)
    };
    var changeTaskStatus = (payload)=>{
      var task = tasklist.value.find((item)=>{
        return item.id == payload;
      })
      if( task ){
        task.status = !task.status;
      }
    };
    var unfinishedCount = (state)=>{
      return tasklist.value.filter((item)=>item.status == false).length
    };
    var delTask = (index)=>{
      tasklist.value.splice(index,1);
    }

  return { tasklist,addTask,changeTaskStatus,unfinishedCount,delTask }
})
